package jdbc;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * @author DCSGO
 * @version 1.0
 * 获取数据库连接的5种方式
 */
@SuppressWarnings ({"all"})
public class JdbcConn {
    static private final String url = "jdbc:mysql://localhost:3306/chapter25";
    static private final String user = "root";
    static private final String password = "dcsgo";
    static private final String className = "com.mysql.jdbc.Driver";
    static private final Properties properties = new Properties();

    static {
        properties.setProperty("user", user);
        properties.setProperty("password", password);
    }

    //代码中显式的加载驱动
    @Test
    public void connect1() throws SQLException {
        com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
        Connection connect = driver.connect(url, properties);
        System.out.println("第一种方式：");
        System.out.println(connect);
        connect.close();
        //第一种方式：
        //com.mysql.jdbc.JDBC4Connection@58a90037
    }

    //通过反射加载驱动
    @Test
    public void connect2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Class<?> CDriver = Class.forName(className);
        //可以将 className 放入一个配置文件中 从而达到 动态加载
        Driver driver = (Driver) CDriver.newInstance();
        //这里的 Driver 是 java.sql.Driver 是一个接口! 不是 com.mysql.jdbc.Driver
        Connection connect = driver.connect(url, properties);
        System.out.println("第二种方式：");
        System.out.println(connect);
        connect.close();
        //第二种方式：
        //com.mysql.jdbc.JDBC4Connection@58a90037
    }

    //反射加载驱动并通过 DriverManager 获取连接
    @Test
    public void connect3() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
        Class<?> CDriver = Class.forName(className);
        Driver driver = (Driver) CDriver.newInstance();
        DriverManager.registerDriver(driver);//注册 Driver 驱动
        //获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("第三种方式：");
        System.out.println(connection);
        connection.close();
        //第三种方式：
        //com.mysql.jdbc.JDBC4Connection@74294adb
    }

    //第四种方式 方式三的简化
    @Test
    public void connect4() throws ClassNotFoundException, SQLException {
        Class.forName(className);//触发类加载
        /*
        public class Driver extends NonRegisteringDriver implements java.sql.Driver {
            public Driver() throws SQLException {
            }

            static {
                try {
                    DriverManager.registerDriver(new com.mysql.jdbc.Driver());
                } catch (SQLException var1) {
                    throw new RuntimeException("Can't register driver!");
                }
            }
        }
        com.mysql.jdbc.Driver 在类加载时就向 DriverManager 进行注册了
        */

        //获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("第四种方式：");
        System.out.println(connection);
        connection.close();
        //第四种方式：
        //com.mysql.jdbc.JDBC4Connection@58a90037

        /*
        1.Mysql驱动5.1.6可以无需Class.forName("com.mysql.jdbc.Driver");
        2.从jdk1.5以后使用了jdbc4,不再需要显示调用class.forName()注册驱动而是自动调用驱动
          jar包下META-INF\services\java.sql.Driver文本中的类名称去注册
        3.建议还是写上Class.forName("com.mysql.jdbc.Driver"),更加明确
         */
    }

    //方式4 的基础上使用配置文件(推荐)
    @Test
    public void connect5() throws IOException, ClassNotFoundException, SQLException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        Class.forName(properties.getProperty("Driver"));
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("第5种方式：");
        System.out.println(connection);
        connection.close();
        //第5种方式：
        //com.mysql.jdbc.JDBC4Connection@58a90037
    }

    //课堂练习
    //使用方式5完成:
    //1.创建news表
    //2.使用jdbc添加5条数据
    //3.修改id=1的记录,将content改成一个新的消息
    //4.删除id=3的记录
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        Class.forName(properties.getProperty("Driver"));
        Connection connection = DriverManager.getConnection(
                properties.getProperty("url"),
                properties.getProperty("user"),
                properties.getProperty("password"));
        Statement statement = connection.createStatement();
        statement.executeUpdate("insert into news values(null,'上海新闻')");
        statement.executeUpdate("insert into news values(null,'北京新闻')");
        statement.executeUpdate("insert into news values(null,'重庆新闻')");
        statement.executeUpdate("insert into news values(null,'天津新闻')");
        statement.executeUpdate("insert into news values(null,'武汉新闻')");
        statement.executeUpdate("update news set content = '国际新闻' where id = 1");
        statement.executeUpdate("delete from news where id = 3");
        statement.close();
        connection.close();
    }
}
